/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gov.decea.projects.sgq.dao;

import gov.decea.projects.sgq.exception.DAOException;
import gov.decea.projects.sgq.model.Aerodromo;
import gov.decea.projects.sgq.util.MessageHelper;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author fernandofot
 */
public class HibernateAerodromoDAO implements AerodromoDAO {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public Long create(Aerodromo aerodromo) throws DAOException {
        Session session = sessionFactory.getCurrentSession();

        try {
            Long id = (Long) session.save(aerodromo);

            return id;
        } catch (HibernateException e) {

            Logger.getLogger(HibernateAerodromoDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("aerodromos.create.error"));

        }
    }

    @Override
    public void update(Aerodromo aerodromo) throws DAOException {
        Session session = sessionFactory.getCurrentSession();

        try {
            session.update(aerodromo);

        } catch (HibernateException e) {

            Logger.getLogger(HibernateAerodromoDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("aerodromos.update.error"));

        }
    }

    @Override
    public void remove(Long id) throws DAOException {
        Session session = sessionFactory.getCurrentSession();

        try {
            Aerodromo aerodromo
                    = (Aerodromo) session.get(Aerodromo.class, id);
            session.delete(aerodromo);

        } catch (HibernateException e) {

            Logger.getLogger(HibernateAerodromoDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("aerodromos.remove.error"));

        }
    }

    @Override
    public Aerodromo find(Long id) throws DAOException {
        Session session = sessionFactory.getCurrentSession();

        try {
            Aerodromo aerodromo = (Aerodromo) session.get(Aerodromo.class, id);
            return aerodromo;

        } catch (HibernateException e) {

            Logger.getLogger(HibernateAerodromoDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("aerodromos.find.unique.error"));

        }
    }

    @Override
    public Aerodromo findBySigla(String sigla) throws DAOException {
        Session session = sessionFactory.getCurrentSession();

        try {
            Aerodromo aerodromo = (Aerodromo) session.createQuery("from Aerodromo a "
                    + "where a.sigla = :sigla")
                    .setString("sigla", sigla)
                    .uniqueResult();
            return aerodromo;

        } catch (HibernateException e) {

            Logger.getLogger(HibernateAerodromoDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("aerodromos.find.unique.error"));

        }
    }

    @Override
    public List<Aerodromo> findAll() throws DAOException {
        Session session = sessionFactory.getCurrentSession();

        try {
            List<Aerodromo> aerodromos
                    = session.createQuery("from Aerodromo")
                    .list();
            return aerodromos;

        } catch (HibernateException e) {

            Logger.getLogger(HibernateAerodromoDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("aerodromos.find.list.error"));

        }
    }

    @Override
    public List<Aerodromo> findAll(Long organizacaoId) throws DAOException {
        Session session = sessionFactory.getCurrentSession();

        try {
            List<Aerodromo> aerodromos
                    = session.createQuery("from Aerodromo a "
                            + "where a.organizacaoId = :organizacaoId")
                    .setLong("organizacaoId", organizacaoId)
                    .list();
            return aerodromos;

        } catch (HibernateException e) {

            Logger.getLogger(HibernateAerodromoDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("aerodromos.find.list.error"));

        }
    }

    @Override
    public List<Aerodromo> search(String term) throws DAOException {
        Session session = sessionFactory.getCurrentSession();

        try {
            List<Aerodromo> aerodromos
                    = session.createQuery("from Aerodromo")
                    .list();
            return aerodromos;

        } catch (HibernateException e) {

            Logger.getLogger(HibernateAerodromoDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("aerodromos.find.list.error"));

        }
    }

}
